package com.qianyu.project.service.smj;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.ProjectSalary;
import com.qianyu.project.mapper.provider.smj.smjLaberSalaryDao;
import com.qianyu.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class smjLaberSalaryServiceImpl implements smjLaberSalaryService {
    @Autowired
    private smjLaberSalaryDao lsd;

    @Override
    public String AddLaberSalary(String p_id, String type, String year, String menth, String apply_date, String handler_id, String attach_ids) {
        String update_date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String[] ids = update_date.split(" ");
        String ids1 =ids[0];
        String ids2 =ids[1];
        String[] ids3 = ids1.split("-");
        String[] ids4 = ids2.split(":");
        String id ="";
        for (int i = 0; i <ids3.length ; i++) {
            id += ids3[i];
        }
        for (int i = 0; i <ids4.length ; i++) {
            id += ids4[i];
        }
        lsd.AddLaberSalary(id, p_id, type, year, menth, apply_date, handler_id, attach_ids);
        return id;
    }

    @Override//添加工资明细
    public void AddWorkList(String salary_id, String name, String type, String day, String salaryday, String salary, String salaryplus, String borrowsalary, String insurancesalary, String deductsalary) {
        String paysalary = Integer.valueOf(salary)+Integer.valueOf(salaryplus)+"";
        String allocatesalary = Integer.valueOf(paysalary)-Integer.valueOf(borrowsalary)-Integer.valueOf(insurancesalary)-Integer.valueOf(deductsalary)+"";
        lsd.AddWorkList(salary_id, name, type, day, salaryday, salary, salaryplus,paysalary, borrowsalary, insurancesalary, deductsalary,allocatesalary);
    }

    @FlowBegin("project_salary")//发起审批
    @Override
    public Integer FlowSalary(@FlowEntry("id") ProjectSalary projectSalary) {
        lsd.UpdateApproveStatus(1, projectSalary.getId());
        return 1;
    }

    @Override//查询工资列表
    public IPage<Map<String, Object>> listlabersalary(Map<String, String> params) {
        IPage<Map<String, Object>> page= PageUtil.createPage(params);
        IPage<Map<String, Object>> list = lsd.listlabersalary(page,params);
        for (int i = 0; i<list.getRecords().size();i++){
           String id=  list.getRecords().get(i).get("id").toString();
           String ynumber = lsd.getYnumber(id);
           if(ynumber == null){
               ynumber ="0";
           }
           String snumber = lsd.getSnumber(id);
            if(snumber == null){
                snumber ="0";
            }
           list.getRecords().get(i).put("ynumber",ynumber);
            list.getRecords().get(i).put("snumber",snumber);
        }
        return list;
    }

    @Override//查询工资明细
    public List<Map<String, Object>> getWorkList(String id) {
        return lsd.getWorkList(id);
    }

    @Override//查询审批过程
    public List<Map<String, Object>> getfromaudit(String id) {
        String fid = lsd.getFid(id);
        return lsd.getfromaudit(fid);
    }
}
